﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.Models;
using Newtonsoft.Json;

namespace Client.Base
{
    public partial class FrmAddProduct : Form
    {
        public FrmAddProduct()
        {
            InitializeComponent();
        }
        public int id;
        public string CName;
        public string Code;
        public string Models;
        public string Unit;
        public string TaxCode;
        public string Tax;
        public int TaxMark;
        public string OpenType;
        public string TraceCode;
        private void FrmAddProduct_Load(object sender, EventArgs e)
        {
            if (OpenType == "new")
            {
                InitWindow();
            }
            else
            {
                tbxCode.Text = Code;
                tbxmodels.Text = Models;
                tbxName.Text = CName;
                tbxUnit.Text = Unit;
                tbxTaxCode.Text = TaxCode;
                tbxTax.Text = Tax;
                cbxMark.SelectedIndex = TaxMark;
            }
        }
        private void InitWindow()
        {
            tbxCode.Text = "";
            tbxmodels.Text = "";
            tbxName.Text = "";
            tbxTax.Text = "";
            tbxTaxCode.Text = "";
            tbxUnit.Text = "";
            cbxMark.Items.Add("否");
            cbxMark.Items.Add("是");


        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                CommodityModel cm = new CommodityModel();
                cm.Code = tbxCode.Text.Trim();
                cm.Models = tbxmodels.Text.Trim();
                cm.CName = tbxName.Text.Trim();
                cm.Tax = tbxTax.Text.Trim();
                cm.TaxCode = tbxTaxCode.Text.Trim();
                cm.Unit = tbxUnit.Text.Trim();
                cm.TaxMark = Convert.ToInt32(cbxMark.SelectedValue);
                if (OpenType == "new")
                {
                    RetModel rm = Share.PostData("51API/Base/CommoidtiesAdd", dc, JsonConvert.SerializeObject(cm));
                    if (rm.Success)
                    {
                        MessageBox.Show("添加成功！");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("添加失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    }
                }
                else
                {
                    RetModel rm = Share.PostData("51API/Base/CommidtiesEdit", dc, JsonConvert.SerializeObject(cm));
                    if (rm.Success)
                    {
                        MessageBox.Show("修改成功！");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("修改失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose();
        }
        /// <summary>
        /// 税收分类编码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            FrmCode fc = new FrmCode();
            fc.Owner = this;
            fc.ShowDialog();
            TraceCode = ;
            fc = null;
        }
    }
}
